package com.sxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxy.controller.common.CustomException;
import com.sxy.dao.DishDao;
import com.sxy.dto.DishDto;
import com.sxy.pojo.Dish;
import com.sxy.pojo.DishFlavor;
import com.sxy.service.DishFlavorService;
import com.sxy.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Value("${syn.path}")
    private String basePath;

    /**
     * 新增菜品，同时保存对应的口味数据
     *
     * @param dishDto
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息
        save(dishDto);

        //获取菜品id
        Long dishId = dishDto.getId();//获取菜品Id

        //对菜品口味数据进行处理

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到数据库表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 通过id查询菜品信息和口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        //拷贝对象
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //查询当前菜品对应的口味信息，
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.eq(DishFlavor::getDishId, dish.getId());
        //执行查询
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        //将查询到的数据封装到dishDto对象中
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理DishFlavor表对应的口味数据
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);
        //重新添加提交的口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //设置口味表中dishId
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品以及对应的口味表
     *
     * @param ids
     */
    @Transactional
    @Override
    public void removeWithFlavor(List<Long> ids) {
        Dish dish;
        //创建条件查询器
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.in(Dish::getId, ids);
        lqw.eq(Dish::getStatus, 1);
        //执行查询,判断是否可以删除
        int count = this.count(lqw);
        if (count > 0) {
            throw new CustomException("菜品未停售~");
        }
        //删除对应的图片
        for (Long id : ids) {
            dish = this.getById(id);
            new File(basePath + dish.getImage()).delete();
        }
        this.removeByIds(ids);

        //创建关联表查询器
        LambdaQueryWrapper<DishFlavor> lqw1 = new LambdaQueryWrapper<>();
        //添加条件
        lqw1.in(DishFlavor::getDishId,ids);

        //执行删除方法
        dishFlavorService.remove(lqw1);

    }

}
